package com.lzw.mall.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.lzw.mall.common.entity.Category;
import com.lzw.mall.common.entity.Product;
import com.lzw.mall.common.mapper.CategoryMapper;
import com.lzw.mall.common.mapper.ProductMapper;
import com.lzw.mall.common.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzw.mall.common.vo.IndexProductVO;
import com.lzw.mall.common.vo.LayuiTableVO;
import com.lzw.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhenwei
 * @since 2023-03-22
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public LayuiTableVO list(Integer page, Integer limit, String keyword,String status) {
        if (status.equals("0")){
            status="未上架";
        }else if (status.equals("1")) {
            status="已上架";
        }
        PageHelper.startPage(page,limit);
        return LayuiTableVO.data(productMapper.selectListPage(keyword,status));
    }

    @Override
    public ResultVO doSave(Product p) {
        ResultVO vo = new ResultVO();
        if (p.getId()==null) {
            vo.setData(baseMapper.insert(p));
        }else {
            updateById(p);
        }
        return vo;
    }

    @Override
    public void doUp(Integer[] ids) {
        productMapper.doUp(ids);
    }

    @Override
    public void doDown(Integer[] ids) {
        productMapper.doDown(ids);
    }

    @Override
    public void doDelete(Integer id) {
        productMapper.doDelete(id);
    }



    @Override
    public List<IndexProductVO> dianxins() {
        List<IndexProductVO> list = new ArrayList<>();


        return list;
    }

    @Override
    public List<IndexProductVO> jianguos() {
        List<IndexProductVO> list = new ArrayList<>();
        List<Category> categories=categoryMapper.last8(17);
        for (Category category: categories){
            //当前分类最便宜的
            Product p=baseMapper.cheap(category.getId());
            if (p!=null) {
                IndexProductVO vo=new IndexProductVO();
                vo.setId(p.getId());
                vo.setName(p.getName());
                vo.setPrice(p.getPrice());
                vo.setHomeImg(p.getHomeImg());
                vo.setTitle(p.getTitle());
                list.add(vo);
            }
        }
        return list;
    }


}
